import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class SelectWindow extends JFrame implements ActionListener {

    private JTextArea ta;
    private JFileChooser jfc = new JFileChooser(new File("."));
    private JButton bOpen, bCopy, bPath, bWorkSpace;
    private JScrollPane ps;
    private File selectFile, targetPath, workSapce;

    public SelectWindow() {
        ta = new JTextArea(15, 30);
        ta.setSelectedTextColor(Color.RED);//选择行标红
        ta.setLineWrap(true);//自动换行
        ta.setWrapStyleWord(true);//换行不换字
        ps = new JScrollPane(ta);
        bOpen = new JButton("选择文件");
        bCopy = new JButton("复制文件");
        bWorkSpace = new JButton("工作空间");
        bPath = new JButton("选择目录");

        bOpen.addActionListener((ActionListener) this);
        bPath.addActionListener((ActionListener) this);
        bCopy.addActionListener((ActionListener) this);
        bWorkSpace.addActionListener((ActionListener) this);

        this.add(ps);
        this.add(bOpen);
        this.add(bWorkSpace);
        this.add(bCopy);
        this.add(bPath);
        this.setTitle("文件选择器的使用");
        this.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 10));
        this.setSize(800, 300);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        ta.setText("工具启动成功！");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jbt = (JButton) e.getSource();
        if (jbt == bOpen) {//选择文件
            try {
                //打开文件选择器对话框
                int status = jfc.showOpenDialog(this);
                //没有选打开按钮结果提示
                if (status != JFileChooser.APPROVE_OPTION) {
                    ta.append("\n没有选中文件");
                } else {
                    //被选中的文件保存为文件对象
                    selectFile = jfc.getSelectedFile();
                    if (!selectFile.isFile() || !selectFile.exists()) {
                        ta.append("\n文件不存在或有误！");
                    }
                    ta.append("\n文件选择成功！文件名：" + selectFile.getName());
                }
            } catch (Exception exception) {
                System.out.println(exception);
                ta.setText("选择文件时出错，错误信息：" + exception);
            }
        }
        if (jbt == bPath) {//选择复制目录
            try {
                if (!selectFile.exists()) {
                    ta.append("\n请先选择文件！");
                }
                jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
                jfc.setDialogTitle("选择文件复制目录");
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置仅可选择目录
                int returnValue = jfc.showSaveDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    if (!jfc.getSelectedFile().isDirectory()) {
                        ta.append("\n选择文件夹有误！");
                        return;
                    }
                    targetPath = jfc.getSelectedFile();
                    ta.append("\n选择目录成功！选择复制目录为：" + targetPath);
                }
            } catch (Exception exception) {
                System.out.println(exception);
                ta.setText("选择目录时出错，错误信息：" + exception);
            }
        }
        if (jbt == bWorkSpace) {
            try {
                jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
                jfc.setDialogTitle("选择工作空间");
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置仅可选择目录
                int returnValue = jfc.showSaveDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    if (!jfc.getSelectedFile().isDirectory()) {
                        ta.append("\n选择文件夹有误！");
                        return;
                    }
                    workSapce = jfc.getSelectedFile();
                    ta.append("\n选择工作空间成功！选择工作空间为：" + workSapce);
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        if (jbt == bCopy) {//复制文件
            try {
                if (!selectFile.exists() || !targetPath.exists()) {
                    ta.append("\n选择的文件或复制目录有误！");
                }
                ta.append("\n开始复制...");
                FileExport fileExport = new FileExport(selectFile, targetPath, workSapce);
                if (!fileExport.copyFile()) {
                    ta.append("\n复制文件出错！");
                }
                ta.append("\n复制文件成功！");
            } catch (Exception exception) {
                System.out.println(exception);
                ta.setText("复制文件时出错，错误信息：" + exception);
            }
        }
    }
}
